package MyApp;

import com.example.myapplication.R;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

public class Enerage extends AppCompatActivity {

    private EditText etWeight;
    private EditText etHeight;
    private EditText etAge;
    private RadioGroup rgGender;
    private Spinner spActivityLevel;
    private TextView tvResult;
    private RadioGroup rgGoal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enerage);

        etWeight = findViewById(R.id.etWeight);
        etHeight = findViewById(R.id.etHeight);
        etAge = findViewById(R.id.etAge);
        rgGender = findViewById(R.id.rgGender);
        spActivityLevel = findViewById(R.id.spActivityLevel);
        tvResult = findViewById(R.id.tvResult);
        rgGoal = findViewById(R.id.rgGoal); // 初始化目标单选按钮组

        spActivityLevel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                calculateCalories();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        findViewById(R.id.btnCalculate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculateCalories();
            }
        });
    }

    private void calculateCalories() {
        try {
            float weight = Float.parseFloat(etWeight.getText().toString());
            float height = Float.parseFloat(etHeight.getText().toString()) / 100; // 转换为米
            int age = Integer.parseInt(etAge.getText().toString());
            float pal = getActivityLevelValue(spActivityLevel.getSelectedItemPosition());

            float bmr = calculateBMR(weight, height, age);
            float tdee = bmr * pal;

            // 计算目标卡路里摄入量
            int goalId = rgGoal.getCheckedRadioButtonId();
            float goalCalories = tdee;
            if (goalId == R.id.rbLoseWeight) {
                goalCalories -= 500; // 减肥目标，减少 500 卡路里
            } else if (goalId == R.id.rbGainWeight) {
                goalCalories += 500; // 增重目标，增加 500 卡路里
            } else {
                // 保持体重目标，使用 TDEE
            }

            // 提供饮食建议
            provideDietAdvice(goalCalories);

            // 更新结果显示
            tvResult.setText(String.format("您的日常能量消耗（TDEE）约为: %.2f 千卡\n您的目标卡路里摄入量约为: %.2f 千卡", tdee, goalCalories));
        } catch (NumberFormatException e) {
            tvResult.setText("请输入有效的数字");
        }
    }

    private float calculateBMR(float weight, float height, int age) {
        float bmr = 0;
        int genderId = rgGender.getCheckedRadioButtonId();
        if (genderId == R.id.rbMale) {
            bmr = (float) (88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age));
        } else if (genderId == R.id.rbFemale) {
            bmr = (float) (447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age));
        } else {
            throw new IllegalArgumentException("请选择性别");
        }
        return bmr;
    }

    private float getActivityLevelValue(int position) {
        switch (position) {
            case 0: return 1.2f; // 久坐
            case 1: return 1.375f; // 轻度活动
            case 2: return 1.55f; // 中度活动
            case 3: return 1.725f; // 非常活跃
            case 4: return 1.9f; // 极端活跃
            default: throw new IllegalArgumentException("无效的活动水平");
        }
    }

    private void provideDietAdvice(float goalCalories) {
        int highCalorieRange = 300; // 每份高卡路里食物的卡路里范围
        int mediumCalorieRange = 200; // 每份中卡路里食物的卡路里范围
        int lowCalorieRange = 100; // 每份低卡路里食物的卡路里范围

        // 计算每种食物的建议份数
        int highCalorieServings = (int) (goalCalories / highCalorieRange);
        int mediumCalorieServings = (int) (goalCalories / mediumCalorieRange);
        int lowCalorieServings = (int) (goalCalories / lowCalorieRange);

        // 提供饮食建议
        String dietAdvice = "饮食建议：\n";
        dietAdvice += "高卡路里食物（每份约 " + highCalorieRange + " 千卡）：" + highCalorieServings + " 份\n";
        dietAdvice += "中卡路里食物（每份约 " + mediumCalorieRange + " 千卡）：" + mediumCalorieServings + " 份\n";
        dietAdvice += "低卡路里食物（每份约 " + lowCalorieRange + " 千卡）：" + lowCalorieServings + " 份\n";
        tvResult.append(dietAdvice);
    }
}